package com.jiangjiang.eshop.promotion.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.promotion.dao.CouponAchieveMapper;
import com.jiangjiang.eshop.promotion.domain.CouponAchieveEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName CouponAchieveService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/22 10:30
 * @Version 1.0
 **/
@Service
public class CouponAchieveService extends ServiceImpl<CouponAchieveMapper, CouponAchieveEntity> {

    /**
     * 查询用户还没有使用过的优惠券领取记录
     * @param userAccountId 用户账户id
     * @return 查询结果
     */
    public List<CouponAchieveEntity> listUnusedByUserAccountId( Long userAccountId){
        LambdaQueryWrapper<CouponAchieveEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CouponAchieveEntity::getUserAccountId, userAccountId)
                .eq(CouponAchieveEntity::getIsUsed, 0)
                .isNull(CouponAchieveEntity::getUsedTime);
        return this.list(wrapper);
    }

    /**
     * 判断优惠券是否被领取过
     * @param couponId 优惠券id
     * @param userAccountId 用户账户id
     * @return
     */
    public Boolean checkAccountHasAchieved(Long couponId, Long userAccountId) {
        LambdaQueryWrapper<CouponAchieveEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CouponAchieveEntity::getCouponId, couponId)
                .eq(CouponAchieveEntity::getUserAccountId, userAccountId);
        return Objects.nonNull(this.getOne(wrapper));
    }
}
